#!/usr/bin/env bash
# 
# Copyright (c) 2014, Joyent, Inc. All rights reserved.
#
# Joyent specific scripts that are ran on each boot
# this is called from /etc/rc.local

# load common functions and vars
. /lib/smartdc/lib_smartdc_scripts.cfg

# DO NOT use lib_smartdc_fatal in here
# You want the rest of the init script to run
# instead use info with ERROR
# this will show up in logs and on console

# CentOS and Fedora
setup_redhat() {
  if [[ ! "$(grep bhyve /etc/product)" ]]; then
  # CentOS does something interesting to the serial devices.
  # We run a dummy query
  if [[ ! -f $MDATA_GET_BIN ]]; then
     lib_smartdc_info "ERROR: File not found $MDATA_GET_BIN"
  fi
    
  $MDATA_GET_BIN dummykey 2>&1 > /dev/null
  if [[ -f /etc/udev/rules.d/70-persistent-net.rules ]] ; then
    rm -f /etc/udev/rules.d/70-persistent-net.rules
  fi
  
  interfaces=$(ifconfig -a | grep ^eth | awk '{print $1}' | tr ":" " ")
  
  for i in ${interfaces[@]} ; do
    ifdown $i
    local config=/etc/sysconfig/network-scripts/ifcfg-$i
    echo "DEVICE=\"$i\"" > $config
    echo "ONBOOT=\"yes\"" >> $config
    echo "BOOTPROTO=\"dhcp\"" >> $config
    ifup $i

    # Fix loopback if configuration doesn't exist
    if [[ ! -f "/etc/sysconfig/network-scripts/ifcfg-lo" ]]; then 
    cat <<-EOF >> /etc/sysconfig/network-scripts/ifcfg-lo 
	DEVICE=lo
	IPADDR=127.0.0.1
	NETMASK=255.0.0.0
	NETWORK=127.0.0.0
	# If you're having problems with gated making 127.0.0.0/8 a martian,
	# you can change this to something else (255.255.255.255, for example)
	BROADCAST=127.255.255.255
	ONBOOT=yes
	NAME=loopback
EOF
    ifup lo
    fi

    # Send gratuitous arp packets to force an arp cache update
    if [[ -f $SEND_ARP_UPDATES_BIN ]]; then 
       out=$($SEND_ARP_UPDATES_BIN) 
    else
       lib_smartdc_info "ERROR: File not found $SEND_ARP_UPDATES_BIN"
    fi
  done
  fi
}

# Debian and Ubuntu
setup_debian() {
  # regenerate SSH keys if they're missing
  which dpkg-reconfigure >/dev/null 2>&1 || lib_smartdc_info "ERROR: \
    dpkg-reconfigure not found" 
  keycount=$(find /etc/ssh -name 'ssh_host_*_key*' | wc -l)
  if [[ $keycount -eq 0 ]] ; then 
    lib_smartdc_info "reconfiguring openssh server"
    dpkg-reconfigure openssh-server
  fi

  local config='/etc/network/interfaces' 
  lib_smartdc_info "creating /etc/network/interfaces"

  echo "#autogenerated by $0" > $config
  echo >> $config
  lib_smartdc_info "adding loopback"
  echo "auto lo" >> $config
  echo "iface lo inet loopback" >> $config
  echo >> $config
  
  interfaces=$(ifconfig -a | grep ^eth | awk '{print $1}' | tr ":" " ")
  for i in ${interfaces[@]} ; do
    ifdown $i
    lib_smartdc_info "adding $i"
    echo "auto $i" >> $config
    echo "iface $i inet dhcp" >> $config
    echo >> $config
    lib_smartdc_info "bringing up $i"
    ifup $i
  done

}

# Start of Main
case `uname -s | tr '[:upper:]' '[:lower:]'` in
  linux)
    if [[ -f /etc/redhat-release ]] ; then
      setup_redhat
    elif [[ -f /etc/debian_version ]] ; then
      setup_debian
    fi
    ;;
  *)
    lib_smartdc_info "ERROR: OS specific features not implemented"
    ;;
esac

# scripts that can run on all systems
(/lib/smartdc/set-root-authorized-keys)
if [[ ! -f /lib/smartdc/.firstboot-complete-do-not-delete ]] ; then
  (/lib/smartdc/firstboot)
fi
(/lib/smartdc/set-hostname)
(/lib/smartdc/run-operator-script)
(/lib/smartdc/get-user-data)
(/lib/smartdc/run-user-script)

if [[ ! -d /var/lock/subsys ]] ; then
  mkdir -p /var/lock/subsys
fi
touch /var/lock/subsys/local

exit 0
